Handle Self wrapped in Annotated when checking the expected self type - #21928
Merged
ilevkivskyi merged 2 commits intoSep 8, 2026
Merged
Conversation
This comment has been minimized.
This comment has been minimized.
A5rocks
approved these changes
Sep 6, 2026
A5rocks
left a comment
Collaborator
There was a problem hiding this comment.
I'm surprised we don't already simply trim out Annotated in all types! But this makes sense to me otherwise.
This comment has been minimized.
This comment has been minimized.
Contributor
Author
|
Out of curiosity(not entirely sure if it's actually an issue), while snooping around to fix this issue, I noticed mypy is okay with this: class Foo:
def fails[T: Self](self: Annotated[T, "tag"], x: int) -> None: passI might be missing a lot of things since I'm fairly new to type checking semantics, but I think I couldn't find anything about this in PEP 673, and no other type checker allows this(I tried pyright, zuban, and ty). Is this intended on mypy's side? |
|
I've also found that the following has the same issue: from typing import Self
type Test[T] = T
class Foo:
# error: Method cannot have explicit self annotation and Self type [misc]
def also_fails(self: Test[Self], x: int) -> None: passNot a practical issue but it does seem odd. |
EmmanuelNiyonshuti
force-pushed
the
fix-expected-annotated-self-type
branch
from
September 8, 2026 19:15
4749787 to
f5696b4
Compare
Contributor
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #21917
Self wrapped in Annotated was not being recognized as an expected self type. This PR updates
is_expected_self_typeto recurse through Annotated when checking unbound types.